IF EXISTS (SELECT * FROM sys.objects WHERE type = 'P' AND name = 'sp_User_SelectPermission')
	DROP PROCEDURE dbo.sp_User_SelectPermission
GO
CREATE PROCEDURE dbo.sp_User_SelectPermission
	@UserID INT
	, @SitemapID NVARCHAR(50)
AS
BEGIN
	SET NOCOUNT OFF

	DECLARE @_UserID INT 
	SET @_UserID = @UserID

	DECLARE @_SitemapID NVARCHAR(50) = ISNULL(@SitemapID, '')

	SELECT
		rp.SitemapID
		, rp.PermissionID
		, IsActived =CONVERT(BIT, MAX(CONVERT(INT, rp.IsActived)))
	FROM
		dbo.RolePermissions rp
		JOIN dbo.UserRoles ur ON ur.RoleID = rp.RoleID
	WHERE
		ur.UserID = @_UserID
		AND (rp.SitemapID = @_SitemapID OR @_SitemapID = '')
	GROUP BY
		rp.SitemapID
		, rp.PermissionID
END
GO